﻿using Forging2.Common.Heating;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Forging2.Common.Clamping
{

    public class Clamping : Equipment, IClamping
    {
        public WorkingPiece ClampWorkpiece { get; private set; }
        public int InOut { get; set; }
        public HeatingFurnace HeatingFurnace { get; set; }
        public Forging.ForgingDevice ForgingDevice { get; set; }
        public Rolling.RollingDevice RollingDevice { get; set; }
        private DataBase.ZH_IMES.V_ForgingOperateData vf;
        //private UpdateWorkpiece updateWorkpiece = new UpdateWorkpiece();
        /// <summary>
        /// 抓取状态0-无抓取，1-抓取中
        /// </summary>
        public int ClampingStauts { get; private set; } = 0;

        private Const.ClampingType ClampingType;


        /// <summary>
        /// 抓取结束，执行此方法
        /// </summary>
        public void ClampingFinshed()
        {
            ClampingStauts = 0;
        }

        /// <summary>
        /// 加热炉取料
        /// </summary>
        /// <param name="heaFurnace">加热炉</param>
        /// <param name="workpiece">加热工序数据</param>       
        /// <returns>数据GID，空-取料失败</returns>
        public async Task<dynamic> FurnaceClamping(Heating.HeatingFurnace heatFurnace, WorkingPiece workpiece)
        {
            if (!ClampOperated(Const.ClampingType.HeatingFurnace, workpiece))
            {
                return  "";
            }
            workpiece.InFurnace = "0";
            var uvr = Const.ZH_IMESDbContext.uex_vtrack_record.Find(workpiece.GID);
            uvr.UDA1 = workpiece.InFurnace;
            uvr.MODIFY_DATE = DateTime.Now;
            uvr.MODIFY_ID = workpiece.Operater;
            uvr.ACTUAL_END_DATE = DateTime.Now;
            //await WorkingPieceToDataBase(workpiece, 0, 0);
            await ClampingOperateLog(workpiece, 0);
            return workpiece.GID;
        }

        public async Task<dynamic> FurnaceLoosen(Heating.HeatingFurnace heatingFurnace, WorkingPiece workpiece)
        {

            if (!LossenOperated(0))
            {
                return "";
            }
            workpiece.InFurnace = "1";
            vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
            vf.InFurnace = workpiece.InFurnace;
            //await WorkingPieceToDataBase(workpiece, 0, 0);
            await ClampingOperateLog(workpiece , 0);
            return workpiece.GID;

            //throw new NotImplementedException();
        }

        public async Task<dynamic> ForgingClamping(Forging.ForgingDevice forgingDevice, WorkingPiece workpiece)
        {
            if (ClampingStauts == 1)
            {
                return "";
            }

            await ClampingOperateLog( workpiece, 1);
            
            return workpiece.GID;
            //return Task.FromResult(await forgingDevice.ForgingFinshed(workpiece));
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 锻压机放料
        /// </summary>
        /// <param name="forgingDevice">锻压机</param>
        /// <param name="workpiece">锻压工序数据</param>
        /// <returns>数据GID，空-放料失败</returns>
        public async Task<dynamic> ForgingLoosen(Forging.ForgingDevice forgingDevice, WorkingPiece workpiece)
        {

            if (!LossenOperated(Const.ClampingType.Forging))
            {
                return "";
            }
            workpiece.DIS_CODE_STATE = 1;
            var  vf = Const.ZH_IMESDbContext.uex_vtrack_record.Find(workpiece.GID);
            vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
            vf.MODIFY_ID = workpiece.Operater;
            vf.MODIFY_DATE = DateTime.Now;
            vf.ACTUAL_BEGIN_DATE = DateTime.Now;
            //await WorkingPieceToDataBase(workpiece, 1, 0);
            await ClampingOperateLog(workpiece, 1);
            return workpiece.GID;



        }

        public async Task<dynamic> RollingClamping(Rolling.RollingDevice rolling, WorkingPiece workpiece)
        {
            if (!ClampOperated(Const.ClampingType.Rolling, workpiece))
            {
                return "";
            }
            ClampWorkpiece = workpiece;

            await ClampingOperateLog( workpiece, 2);

            //await rolling.RollingFinshed(workpiece);
            return workpiece.GID;
            //throw new NotImplementedException();
        }

        public async Task<dynamic> RollingLoosen(Rolling.RollingDevice rolling, WorkingPiece workpiece)
        {
            if (!LossenOperated(Const.ClampingType.Rolling))
            {
                return "";
            }

            await ClampingOperateLog( workpiece, 2);

            //await rolling.RollingBeging(workpiece);
            return workpiece.GID;

            //throw new NotImplementedException();
        }

        public async Task<string> OhtersLoosen(WorkingPiece workpiece)
        {
            if (!LossenOperated(Const.ClampingType.Others))
            {
                return "";
            }
            workpiece.DIS_CODE_STATE = 1;
            vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
            vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
            vf.MODIFY_ID = workpiece.Operater;
            vf.MODIFY_DATE = DateTime.Now;
            vf.ACTUAL_BEGIN_DATE = DateTime.Now;
            //await WorkingPieceToDataBase(workpiece, 1, 0);
            await ClampingOperateLog(workpiece, 3);
            return workpiece.GID;
        }
        public  async Task<String> OthersClampingAsync(WorkingPiece workpiece)
        {
            if (!ClampOperated(Const.ClampingType.Others, workpiece))
            {
                return "";
            }
            workpiece.InFurnace = "0";
            vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
            vf.InFurnace = workpiece.InFurnace;
            vf.MODIFY_DATE = DateTime.Now;
            vf.MODIFY_ID = workpiece.Operater;
            vf.ACTUAL_END_DATE = DateTime.Now;
            //await WorkingPieceToDataBase(workpiece, 0, 0);
            await ClampingOperateLog(workpiece, 3);
            return workpiece.GID;
        }
        private Boolean ClampOperated(Const.ClampingType clamptype, WorkingPiece workpiece)
        {
            string msg;
            if (ClampingStauts == 1)
            {
                msg = "取料装置已有工件，不能取料！";
                return false;
            }
            ClampingStauts = 1;
            ClampWorkpiece = workpiece;
            string clampname = "";
            switch (clamptype)
            {
                case Const.ClampingType.HeatingFurnace:
                    clampname = "加热炉";
                    break;
                case Const.ClampingType.Forging:
                    clampname = "锻压机";
                    break;
                case Const.ClampingType.Rolling:
                    clampname = "辗环机";
                    break;

                default:
                    clampname = "其他";
                    break;
            }
            msg = string.Format(" {0} 取料", clampname);
            ClampWorkpiece = workpiece;
            Const.Front.Notification = msg;
            return true;
        }
        private Boolean LossenOperated(Const.ClampingType clamptype)
        {
            string msg;
            if (ClampingStauts == 0)
            {
                msg = "取料装置没有工件，不能放料！";
                return false;
            }
            ClampingType = clamptype;
            ClampingStauts = 0;
            ClampWorkpiece = null;
            string clampname = "";
            switch (clamptype)
            {
                case Const.ClampingType.HeatingFurnace:
                    clampname = "加热炉";
                    break;
                case Const.ClampingType.Forging:
                    clampname = "锻压机";
                    break;
                case Const.ClampingType.Rolling:
                    clampname = "辗环机";
                    break;
                case Const.ClampingType.Others:
                    clampname = "其他";
                    break;
                default:
                    break;
            }
            msg = string.Format(" 放料  {0}", clampname);
            Common.Const.Front.Notification = msg;
            return true;
        }

        
        /// <summary>
        /// 取料机操作记录
        /// </summary>
        /// <param name="workpiece">工件信息</param>
        /// <param name="clamptype">抓取的类型，0-加热炉，1-锻压机，2-辗环机</param>
        /// <returns></returns>
        private  Task<int> ClampingOperateLog(WorkingPiece workpiece, byte clamptype)
        {
            var clamplog = new DataBase.QID_IGDATA_ZY.dy_clamp_log()
            {
                ClampOperate_GID = Guid.NewGuid(),
                EquipmentGID = workpiece.WORK_CELL_GID,
                EquipmentCode = workpiece.WORKCELL_CODE,
                VrackGID = workpiece.GID,
                WorkPieceTemperature = workpiece.OutFurnaceTemperatures,
                OperateType = clamptype,
                OperateDate = DateTime.Now,
                IsDelete = 0,
                OperateState = 0
            };
            Const.IGDataDbContext.dy_clamp_log.Add(clamplog);
            int iret= Const.IGDataDbContext.SaveChanges();
            return Task.FromResult(iret);

        }
        /*
        private Task<int> WorkingPieceToDataBase(WorkingPiece workingPiece, byte clamptype, int wpio)
        {
            
            var t1 = ClampingOperateLog(workingPiece, clamptype);
            t1.Wait();
            int t = t1.Result;
            var twp = new Task(() =>
            {
                workingPiece.InFurnace = wpio.ToString();
                //var iret = Const.ZH_IMESDbContext.SaveChanges();
                
            });
            twp.Start();
            twp.Wait();
            t = t + twp.Result;
            Console.WriteLine(string.Format("更新了 {0} 数据", t));
            Const.Front.Notification = string.Format("更新了 {0} 数据", t);
            return Task.FromResult<int>(t);
            
        }

        */
    }
}
